package com.ht.log.ui;

import javax.swing.table.AbstractTableModel;
import javax.swing.event.TableModelEvent;


/**
 * This class extends form AbstractTableModel to provide table model showing a table.
 *
 * @author Wangjiaxiang
 *         Date: 2010-4-13
 */
public class MyTableModel extends AbstractTableModel {
    private Object data[][];
    /**
     * The column names array.
     */
    protected String[] columnNames;

    private Class[] type;

    /**
     * Creates a new instance of MapTableModel.
     */
    public MyTableModel() {
        this(new Object[][]{}, new String[]{""});
    }

    /**
     * Creates a new instance of MapTableModel.
     */
    public MyTableModel(Object[][] data, String[] names) {
        super();
        setData(data);
        setColumnNames(names);
    }

    // ------------------------------------------------------------------------
    // --- methods                                                          ---
    // ------------------------------------------------------------------------

    /**
     * Returns the row count.
     */
    public int getRowCount() {
        return data.length;
    }

    /**
     * Returns the column count.
     */
    public int getColumnCount() {
        return columnNames.length;
    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        if (getColumnClass(columnIndex) == Boolean.class)
            return true;
        return false;
    }

    public Class<?> getColumnClass(int columnIndex) {
        return type[columnIndex];
    }

    public void setColumnClass(Class[] type) {
        this.type = type;
    }

    /**
     * Returns the value at.
     */
    public Object getValueAt(int row, int column) {
        return data[row][column];
    }

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        data[rowIndex][columnIndex] = aValue;
        if (getColumnClass(columnIndex) == Boolean.class) {
            objectSelecteChanged(Boolean.valueOf(aValue.toString()), rowIndex, columnIndex);
        }
    }

    public void objectSelecteChanged(boolean aValue, int rowIndex, int columnIndex) {

    }


    /**
     * Returns the column name.
     */
    public String getColumnName(int column) {
        return columnNames[column];
    }

    /**
     * Sets the column names.
     */
    public void setColumnNames(String[] names) {
        columnNames = names;
    }

    /**
     * Sets the map.
     */

    public void setData(Object[][] data) {
        this.data = data;
        fireTableDataChanged();
    }

    public void addRow(Object[] rowData) {
        Object[][] newData;
        if (data != null) {
            newData = new Object[data.length + 1][getColumnCount()];
            System.arraycopy(data, 0, newData, 0, data.length);
            System.arraycopy(rowData, 0, newData[data.length], 0, rowData.length);
        } else {
            newData = new Object[1][getColumnCount()];
            System.arraycopy(rowData, 0, newData, 0, rowData.length);
        }
        setData(newData);
    }

    public void remove(int rowCount) {
        if (data == null)
            return;
        if(data.length == 1) {
            setData(new Object[0][0]);
            return;
        }
        Object[][] newData;
        newData = new Object[data.length - 1][getColumnCount()];
        System.arraycopy(data, 0, newData, 0, rowCount);
        System.arraycopy(data, rowCount+1, newData, rowCount, data.length - rowCount - 1);
        setData(newData);
    }

    public Object[][] getData() {
        return data;
    }
} // end MapTableModel